package com.example.mydemo;

import org.apache.poi.ss.formula.functions.T;
import org.junit.Test;
import sun.net.www.protocol.jar.URLJarFile;

import java.io.*;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.sql.SQLOutput;
import java.util.*;
import java.util.function.Consumer;
import java.util.jar.JarEntry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tangpengcheng
 * @description：java study
 * @date 2020-07-02 19:37
 */
public class Demo01 {

    public static void main(String[] args) {

        Demo01 demo01 = new Demo01();
        demo01.heapOutOfMemoryError();

    }


    /**
     * 内存溢出
     * vmArgs -Xmx10m -Xms10m -Xmn5m -XX:PermSize=5m -XX:MaxPermSize=5m -XX:SurvivorRatio=8 -XX:MaxDirectMemorySize=2m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=c:\temp\ -XX:+PrintGCDetails
     */

        /**
         * 大对象申请不到空间溢出
         */
        //@Test
        public void heapOutOfMemoryError() {
            byte[] bufOne = new byte[1024 * 1024 * 5];
//            byte[] bufTwo = new byte[1024 * 1024 * 4];
        }

        /**
         * 常量池内存溢出
         * 只要把常量池中的对象加入引用无法被jvm回收即可
         * 1.7 将常量池移入到heap区域，所以显示的是heap 溢出
         * 若是1.6则显示 java.lang.OutOfMemoryError: PermGen space
         */
        //@Test
        public void constantsPoolOutOfMemeryError() {
            long i = 0;
            String a = "";
            List<String> strings = new ArrayList<>(100000);
            for (;;) {
                a = getClass() + String.valueOf(i);
                a.intern();
                strings.add(a);
                i ++;
            }
        }

        /**
         * 栈溢出 递归调用达到栈的最大深度
         */
        //@Test
        public void StackOverflowError() {
            StackOverflowError();
        }
        /**
         * 永久代存储的是类信息，只要加载无限多的类则永久代溢出
         */
        //@Test
        public void permGenOutOfMemoryError() throws Exception{
            URLJarFile urlJarFile = new URLJarFile(new File("C:\\Program Files\\Java\\jdk1.7.0_71\\jre\\lib\\rt.jar"));
            Enumeration<JarEntry> jarEntry = urlJarFile.entries();
            for (JarEntry entry = null; jarEntry.hasMoreElements();entry = jarEntry.nextElement()) {
                String clsName = String.valueOf(entry);
                if (clsName != "null" && !clsName.contains("META-INF") && !clsName.contains("$")) {
                    String finalName = clsName.replace("/",".");
                    finalName = finalName.substring(0,finalName.indexOf(".class"));
                    Class<?> cls = getClass().getClassLoader().loadClass(finalName);
                    if (cls.getDeclaringClass() != null && cls.getDeclaringClass().getModifiers() == Modifier.PUBLIC) {
                        cls.newInstance();
                    }
                }
            }
        }

        /**
         * DirectMemory 默认不指定则跟java堆最大值相等 当然也受限于本身电脑内存
         * 指定MaxDirectMemorySize=2m
         * @throws Exception
         */
        @Test
        public void directBufferOutOfMemoryError() throws Exception{
            ByteBuffer.allocateDirect(2 * 1024 * 1024);
            // 下面使用unsafe类分配内存，则只受限于电脑内存
        /*Field field = Unsafe.class.getDeclaredFields()[0];
        field.setAccessible(true);
        Unsafe unsafe = (Unsafe)field.get(new Object());
        unsafe.allocateMemory(100 * 1024 * 1024);*/
        }

        /**
        * @Description: 集合的arrays测试
        * @Param:  
        * @return:  
        * @Author: PengCheng Tang
        * @Date: 2020/7/2
        */
        @Test
        public void test01(){
            List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
            System.out.println(list);
            Collections.sort(list);
            System.out.println(list);
            Collections.shuffle(list);
            System.out.println(list);
            Integer[] s = (Integer[]) list.toArray();
            System.out.println(Arrays.toString(s));
        }


    @Test
    public void test02() throws IOException {
        File file = new File("C:\\Users\\12200\\Desktop\\数据库信息.txt");

        InputStream inp = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(inp, StandardCharsets.UTF_8);

        BufferedReader bf = new BufferedReader( isr);
        String line = "";
        while ((line = bf.readLine()) != null) {
            // 如果 t x t文件里的路径 不包含---字符串       这里是对里面的内容进行一个筛选
            if (line.lastIndexOf("---") < 0) {
                System.out.println(line);
            }
        }
        bf.close();
        isr.close();
        inp.close();

    }


    @Test
    public  void lambdaTest() {
        ArrayList<Integer> list = new ArrayList<>();

        Collections.addAll(list, 1,2,3,4,5);

        //lambda表达式 方法引用
        Consumer<Integer> b =  (Integer element) -> {
                System.out.println(element);
        };

        list.forEach(b);

        list.forEach(System.out::println);

        Consumer<Integer> a = element -> {
            if (element % 2 == 0) {
                System.out.println(element);
            }
        };
        list.forEach(a);
    }

    @Test
    public void testRegex(){

        System.out.println(isPhone("18180628909"));
        System.out.println(isPhone("19180628909"));
        System.out.println(isPhone("17818062890"));
        System.out.println(isPhone("16818062890"));
        System.out.println(isPhone("15818062890"));
        System.out.println(isPhone("14818062890"));
    }

    public static boolean isPhone(String phone) {
        String regex = "^1[0-9]{10}$";
        if (phone.length() != 11) {
            System.out.println("手机号应为11位数");
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
                System.out.println("请填入正确的手机号");
            }
            return isMatch;
        }
    }




}
